<template>
  <el-dialog
      top="5vh"
      width="700px"
      title="添加/编辑"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    >
    </SubmitForm>
  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  components: {
  },
  data() {
    return {
      apiKey: 'device',
      dialogValue: false,
      ruleForm: {
        id: undefined,
      },
      defaultForm: {
        id: undefined,
        deviceName: '',
        deviceNo: '',
        deviceCarNo: '',
        ratedOutputPower: '',
        outputCurrentRange: '',
        accordingGb: '',
        deviceType: 1,
        currentForm: '直流',
        sort: 0,
        enabled: 1
      },
      rules: {
        deviceName: [
          { required: true, message: '请输入设备名称', trigger: 'blur' }
        ],
        deviceNo: [
          { required: true, message: '请输入设备编号', trigger: 'blur' }
        ],
        deviceCarNo: [
          { required: true, message: '请输入车位号', trigger: 'blur' }
        ],
        ratedOutputPower: [
          { required: true, message: '请输入输出功率', trigger: 'blur' }
        ],
        outputCurrentRange: [
          { required: true, message: '请输入电压', trigger: 'blur' }
        ],
        accordingGb: [
          { required: true, message: '请输入国标', trigger: 'blur' }
        ],
        deviceType: [
          { required: true, message: '请选择充电速度', trigger: 'blur' }
        ],
        currentForm: [
          { required: true, message: '请选择电源类型', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入顺序', trigger: 'blur' }
        ]
      },
    }
  },
  computed: {
    formList() {
      return [
        {
          label: '设备名称',
          prop: 'deviceName',
          name: 'input',
          clearable: true,
          colSpan: 12
        },
        {
          label: '设备编号',
          prop: 'deviceNo',
          name: 'input',
          clearable: true,
          disabled: () => this.ruleForm.id ? true : false,
          colSpan: 12
        },
        {
          label: '车位号',
          prop: 'deviceCarNo',
          name: 'input',
          clearable: true,
          colSpan: 12
        },
        {
          label: '输出功率',
          prop: 'ratedOutputPower',
          name: 'input',
          type: 'number',
          colSpan: 12,
          clearable: true
        },
        {
          label: '电压',
          prop: 'outputCurrentRange',
          name: 'input',
          type: 'number',
          colSpan: 12,
          clearable: true
        },
        {
          label: '国标',
          prop: 'accordingGb',
          name: 'input',
          colSpan: 12,
          clearable: true
        },

        {
          label: '充电速度',
          prop: 'deviceType',
          name: 'radio',
          clearable: true,
          options: [
            {
              label: '快充',
              value: 1
            },
            {
              label: '慢充',
              value: 0
            }
          ],
          colSpan: 12
        },
        {
          label: '电源类型',
          prop: 'currentForm',
          name: 'radio',
          clearable: true,
          colSpan: 12,
          options: [
            {
              label: '直流',
              value: '直流'
            },
            {
              label: '交流',
              value: '交流'
            }
          ]
        },
        {
          label: '顺序',
          prop: 'sort',
          name: 'input',
          type: 'number',
          colSpan: 12,
          clearable: true
        }
      ]
    }

  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
      console.log(this.ruleForm)
    },
    onSave() {
      if (!this.$route.params.id) {
        return
      }
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, { ...this.ruleForm, plotId: this.$route.params.id }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    },
  }
}
</script>

<style scoped lang="scss">
</style>
